<template>
  <div>
    <el-dialog :before-close="handleClose" :visible.sync="showDept" :title="title" width="500px">
      <el-form ref="form" inline :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="部门排序" prop="deptSort">
          <el-input-number v-model.number="form.deptSort" :min="0" :max="999" controls-position="right" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="顶级部门">
          <el-radio-group v-model="form.isTop" style="width: 140px">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="enabled">
          <el-radio v-for="item in dept_status" :key="item.id" v-model="form.enabled" :label="item.value">{{ item.label }}</el-radio>
        </el-form-item>
        <el-form-item v-if="form.isTop === '0'" style="margin-bottom: 0;" label="上级部门" prop="pid">
          <treeselect v-model="form.pid" :load-options="loadDepts" :options="depts" style="width: 370px;" placeholder="选择上级类目" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submitDept">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import { getDepts, getDeptSuperior, add, edit } from '@/api/system/dept'
export default {
  components: { Treeselect },
  props: {
    title: {
      type: String,
      default: ''
    },
    showDept: {
      type: Boolean,
      default: false
    },
    editData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      depts: [],
      dept_status: [
        { label: '启用', id: '1', value: 'true' },
        { label: '停用', id: '2', value: 'false' }
      ],
      menus: [],
      form: {
        isTop: '1',
        name: null,
        deptSort: 999,
        enabled: 'true',
        pid: null,
        subCount: 0,
        id: null
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        deptSort: [
          { required: true, message: '请输入序号', trigger: 'blur', type: 'number' }
        ]
      }
    }
  },
  watch: {
    showDept() {
      if (this.showDept) {
        const params = Object.assign(this.form, this.editData)
        console.log('editData', params)
        if (this.title === '编辑部门') {
          if (params.pid !== null) {
            params.isTop = '0'
          } else if (params.id !== null) {
            params.isTop = '1'
          }
        }
        params.enabled = `${params.enabled}`
        if (params.id != null) {
          this.getSupDepts(params.id)
        } else {
          this.getDeptsList()
        }
      }
    }
  },
  methods: {
    getSupDepts(id) {
      getDeptSuperior(id).then(res => {
        this.depts = res.list.map(function(obj) {
          if (obj.hasChildren && !obj.children) {
            obj.children = null
          }
          return obj
        })
      })
    },
    // 获取弹窗内部门数据
    loadDepts({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        getDepts({ enabled: true, pid: parentNode.id }).then(res => {
          parentNode.children = res.content.map(function(obj) {
            if (obj.hasChildren) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 100)
        })
      }
    },
    getDeptsList() {
      getDepts({ enabled: true }).then(res => {
        this.depts = res.list.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    handleClose() {
      this.$emit('update:showDept', false)
    },
    submitDept() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title === '新增部门') {
            this.addDept()
          } else this.editDept()
        } else {
          return false
        }
      })
    },
    addDept() {
      add({ ...this.form }).then(res => {
        this.handleClose()
        this.$refs['form'].resetFields()
        this.$emit('search')
        this.$message.success('操作成功~')
      })
    },
    editDept() {
      edit({ ...this.form }).then(res => {
        this.handleClose()
        this.$refs['form'].resetFields()
        this.$emit('search')
        this.$message.success('操作成功~')
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  /deep/.el-form-item--small.el-form-item{
    margin-bottom: 25px;
  }
</style>
